package cn.webrx.bs.service;

import cn.webrx.bs.entity.BookCates;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-28 11:51
 */
public class BookCatesImpl implements BookCatesDao {
    @Override
    public BookCatesDao queryById(int id) {
        return null;
    }

    @Override
    public int add(BookCates c) {
        return add(c.getCname(), c.getParentid(), c.getCpath());
    }

    @Override
    public int add(String cname, int parentid, String cpath) {
        int num = 0;
        try {
            num = run.update("insert into bs_bookcates values(null,?,?,?)", cname, parentid, cpath);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public List<BookCates> queryAll() {
        List<BookCates> list = null;
        try {
            list = run.query("SELECT id,cname,parentid,CONCAT_WS('',cpath,id) cpath FROM bs_bookcates ORDER BY cpath", new BeanListHandler<>(BookCates.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteById(int id) {
        return 0;
    }

    @Override
    public int update(BookCates c) {
        return 0;
    }


    @Override
    public List<BookCates> queryByParentId(int id) {
        List<BookCates> list = null;
        try {
            list = run.query("select * from bs_bookcates where parentid=?", new BeanListHandler<>(BookCates.class), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
